home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / rpm / rpmfile.h < prev    next >
Encoding:
C/C++ Source or Header  |  2008-10-22  |  10.1 KB  |  350 lines

  1. /*
  2.  * Copyright (c) Ian F. Darwin 1986-1995.
  3.  * Software written by Ian F. Darwin and others;
  4.  * maintained 1995-present by Christos Zoulas and others.
  5.  * 
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions
  8.  * are met:
  9.  * 1. Redistributions of source code must retain the above copyright
  10.  *    notice immediately at the beginning of the file, without modification,
  11.  *    this list of conditions, and the following disclaimer.
  12.  * 2. Redistributions in binary form must reproduce the above copyright
  13.  *    notice, this list of conditions and the following disclaimer in the
  14.  *    documentation and/or other materials provided with the distribution.
  15.  *  
  16.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  17.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19.  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
  20.  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  22.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26.  * SUCH DAMAGE.
  27.  */
  28. /*
  29.  * file.h - definitions for file(1) program
  30.  * @(#)$Id: file.h,v 1.72 2005/10/17 15:36:22 christos Exp $
  31.  */
  32.  
  33. #ifndef __file_h__
  34. #define __file_h__
  35.  
  36. #ifdef HAVE_CONFIG_H
  37. #include <config.h>
  38. #endif
  39.  
  40. #include <stdio.h>    /* Include that here, to make sure __P gets defined */
  41. #include <errno.h>
  42. #include <fcntl.h>    /* For open and flags */
  43. #ifdef HAVE_STDINT_H
  44. #include <stdint.h>
  45. #endif
  46. #ifdef HAVE_INTTYPES_H
  47. #include <inttypes.h>
  48. #endif
  49. /* Do this here and now, because struct stat gets re-defined on solaris */
  50. #include <sys/stat.h>
  51.  
  52. #ifndef MAGIC
  53. #define MAGIC "/etc/magic"
  54. #endif
  55.  
  56. #ifdef __EMX__
  57. #define PATHSEP    ';'
  58. #else
  59. #define PATHSEP    ':'
  60. #endif
  61.  
  62. #define private static
  63. #ifndef protected
  64. #define protected
  65. #endif
  66. #define public
  67.  
  68. #ifndef HOWMANY
  69. # define HOWMANY (256 * 1024)    /* how much of the file to look at */
  70. #endif
  71. #define MAXMAGIS 8192        /* max entries in /etc/magic */
  72. #define MAXDESC    64        /* max leng of text description */
  73. #define MAXstring 32        /* max leng of "string" types */
  74.  
  75. #define MAGICNO        0xF11E041C
  76. #define VERSIONNO    2
  77. #define FILE_MAGICSIZE    (32 * 4)
  78.  
  79. #define    FILE_LOAD    0
  80. #define FILE_CHECK    1
  81. #define FILE_COMPILE    2
  82.  
  83. struct magic {
  84.     /* Word 1 */
  85.     uint16_t cont_level;    /* level of ">" */
  86.     uint8_t nospflag;    /* supress space character */
  87.     uint8_t flag;
  88. #define INDIR    1        /* if '>(...)' appears,  */
  89. #define    UNSIGNED 2        /* comparison is unsigned */
  90. #define OFFADD    4        /* if '>&' appears,  */
  91. #define INDIROFFADD    8    /* if '>&(' appears,  */
  92.     /* Word 2 */
  93.     uint8_t reln;        /* relation (0=eq, '>'=gt, etc) */
  94.     uint8_t vallen;        /* length of string value, if any */
  95.     uint8_t type;        /* int, short, long or string. */
  96.     uint8_t in_type;    /* type of indirrection */
  97. #define             FILE_BYTE    1
  98. #define                FILE_SHORT    2
  99. #define                FILE_LONG    4
  100. #define                FILE_STRING    5
  101. #define                FILE_DATE    6
  102. #define                FILE_BESHORT    7
  103. #define                FILE_BELONG    8
  104. #define                FILE_BEDATE    9
  105. #define                FILE_LESHORT    10
  106. #define                FILE_LELONG    11
  107. #define                FILE_LEDATE    12
  108. #define                FILE_PSTRING    13
  109. #define                FILE_LDATE    14
  110. #define                FILE_BELDATE    15
  111. #define                FILE_LELDATE    16
  112. #define                FILE_REGEX    17
  113. #define                FILE_BESTRING16    18
  114. #define                FILE_LESTRING16    19
  115. #define                FILE_SEARCH    20
  116.  
  117. #define                FILE_FORMAT_NAME    \
  118. /* 0 */             "invalid 0",        \
  119. /* 1 */                "byte",            \
  120. /* 2 */             "short",        \
  121. /* 3 */             "invalid 3",        \
  122. /* 4 */             "long",            \
  123. /* 5 */             "string",        \
  124. /* 6 */             "date",            \
  125. /* 7 */             "beshort",        \
  126. /* 8 */             "belong",        \
  127. /* 9 */             "bedate",        \
  128. /* 10 */             "leshort",        \
  129. /* 11 */             "lelong",        \
  130. /* 12 */             "ledate",        \
  131. /* 13 */             "pstring",        \
  132. /* 14 */             "ldate",        \
  133. /* 15 */             "beldate",        \
  134. /* 16 */             "leldate",        \
  135. /* 17 */             "regex",        \
  136. /* 18 */            "bestring16",        \
  137. /* 19 */            "lestring16",        \
  138. /* 20 */             "search",
  139.  
  140. #define    FILE_FMT_NUM    "cduxXi"
  141. #define FILE_FMT_STR    "s"    
  142.  
  143. #define                FILE_FORMAT_STRING    \
  144. /* 0 */             NULL,            \
  145. /* 1 */                FILE_FMT_NUM,        \
  146. /* 2 */             FILE_FMT_NUM,        \
  147. /* 3 */             NULL,            \
  148. /* 4 */             FILE_FMT_NUM,        \
  149. /* 5 */             FILE_FMT_STR,        \
  150. /* 6 */             FILE_FMT_STR,        \
  151. /* 7 */             FILE_FMT_NUM,        \
  152. /* 8 */             FILE_FMT_NUM,        \
  153. /* 9 */             FILE_FMT_STR,        \
  154. /* 10 */             FILE_FMT_NUM,        \
  155. /* 11 */             FILE_FMT_NUM,        \
  156. /* 12 */             FILE_FMT_STR,        \
  157. /* 13 */             FILE_FMT_STR,        \
  158. /* 14 */             FILE_FMT_STR,        \
  159. /* 15 */             FILE_FMT_STR,        \
  160. /* 16 */             FILE_FMT_STR,        \
  161. /* 17 */             FILE_FMT_STR,        \
  162. /* 18 */            FILE_FMT_STR,        \
  163. /* 19 */            FILE_FMT_STR,        \
  164. /* 20 */            FILE_FMT_STR,
  165.  
  166.     /* Word 3 */
  167.     uint8_t in_op;        /* operator for indirection */
  168.     uint8_t mask_op;    /* operator for mask */
  169.     uint8_t dummy1;    
  170.     uint8_t dummy2;    
  171. #define                FILE_OPS    "&|^+-*/%"
  172. #define                FILE_OPAND    0
  173. #define                FILE_OPOR    1
  174. #define                FILE_OPXOR    2
  175. #define                FILE_OPADD    3
  176. #define                FILE_OPMINUS    4
  177. #define                FILE_OPMULTIPLY    5
  178. #define                FILE_OPDIVIDE    6
  179. #define                FILE_OPMODULO    7
  180. #define                FILE_OPINVERSE    0x40
  181. #define                FILE_OPINDIRECT    0x80
  182.     /* Word 4 */
  183.     uint32_t offset;    /* offset to magic number */
  184.     /* Word 5 */
  185.     int32_t in_offset;    /* offset from indirection */
  186.     /* Word 6 */
  187.     uint32_t mask;    /* mask before comparison with value */
  188.     /* Word 7 */
  189.     uint32_t dummy3;
  190.     /* Word 8 */
  191.     uint32_t dummp4;
  192.     /* Words 9-16 */
  193.     union VALUETYPE {
  194.         uint8_t b;
  195.         uint16_t h;
  196.         uint32_t l;
  197.         char s[MAXstring];
  198.         struct {
  199. /*@relnull@*/
  200.             char *buf;
  201.             size_t buflen;
  202.         } search;
  203.         uint8_t hs[2];    /* 2 bytes of a fixed-endian "short" */
  204.         uint8_t hl[4];    /* 4 bytes of a fixed-endian "long" */
  205.     } value;        /* either number or string */
  206.     /* Words 17..31 */
  207.     char desc[MAXDESC];    /* description */
  208. };
  209.  
  210. #define BIT(A)   (1 << (A))
  211. #define STRING_IGNORE_LOWERCASE        BIT(0)
  212. #define STRING_COMPACT_BLANK        BIT(1)
  213. #define STRING_COMPACT_OPTIONAL_BLANK    BIT(2)
  214. #define CHAR_IGNORE_LOWERCASE        'c'
  215. #define CHAR_COMPACT_BLANK        'B'
  216. #define CHAR_COMPACT_OPTIONAL_BLANK    'b'
  217.  
  218.  
  219. /* list of magic entries */
  220. struct mlist {
  221.     struct magic *magic;        /* array of magic entries */
  222.     uint32_t nmagic;            /* number of entries in array */
  223.     int mapped;  /* allocation type: 0 => apprentice_file
  224.               *                  1 => apprentice_map + malloc
  225.               *                  2 => apprentice_map + mmap */
  226. /*@dependent@*/
  227.     struct mlist *next, *prev;
  228. };
  229.  
  230. struct magic_set {
  231. /*@relnull@*/
  232.     struct mlist *mlist;
  233.     struct cont {
  234.     size_t len;
  235. /*@relnull@*/
  236.     int32_t *off;
  237.     } c;
  238.     struct out {
  239.     /* Accumulation buffer */
  240. /*@owned@*/ /*@relnull@*/
  241.     char *buf;
  242. /*@dependent@*/ /*@relnull@*/
  243.     char *ptr;
  244.     size_t left;
  245.     size_t size;
  246.     /* Printable buffer */
  247. /*@only@*/
  248.     char *pbuf;
  249.     size_t psize;
  250.     } o;
  251.     int error;
  252.     int flags;
  253.     int haderr;
  254.     const char *file;
  255.     size_t line;
  256. };
  257.  
  258. struct stat;
  259. /*@observer@*/
  260. protected const char *file_fmttime(uint32_t, int)
  261.     /*@*/;
  262. protected int file_buffer(struct magic_set *ms, int, const void *, size_t)
  263.     /*@globals fileSystem, internalState @*/
  264.     /*@modifies ms, fileSystem, internalState @*/;
  265. protected int file_fsmagic(struct magic_set *ms, /*@null@*/ const char *fn, struct stat *sb)
  266.     /*@modifies ms, sb @*/;
  267. protected int file_pipe2file(struct magic_set *ms, int fd, const void *startbuf, size_t nbytes)
  268.     /*@globals errno, fileSystem, internalState @*/
  269.     /*@modifies ms, errno, fileSystem, internalState @*/;
  270. protected int file_printf(struct magic_set *ms, const char *, ...)
  271.     /*@modifies ms @*/;
  272. protected int file_reset(struct magic_set *ms)
  273.     /*@modifies ms @*/;
  274. protected int file_tryelf(struct magic_set *ms, int, const unsigned char *, size_t)
  275.     /*@globals fileSystem, internalState @*/
  276.     /*@modifies ms, fileSystem, internalState @*/;
  277. protected int file_zmagic(struct magic_set *ms, int, const unsigned char *, size_t)
  278.     /*@globals fileSystem, internalState @*/
  279.     /*@modifies ms, fileSystem, internalState @*/;
  280. protected int file_ascmagic(struct magic_set *ms, const unsigned char *, size_t)
  281.     /*@modifies ms @*/;
  282. protected int file_is_tar(struct magic_set *ms, const unsigned char *, size_t)
  283.     /*@modifies ms @*/;
  284. protected int file_softmagic(struct magic_set *ms, const unsigned char *, size_t)
  285.     /*@globals fileSystem @*/
  286.     /*@modifies ms, fileSystem @*/;
  287. /*@null@*/
  288. protected struct mlist *file_apprentice(struct magic_set *ms, const char *, int)
  289.     /*@globals fileSystem, internalState @*/
  290.     /*@modifies ms, fileSystem, internalState @*/;
  291. protected uint32_t file_signextend(struct magic_set *ms, struct magic *, uint32_t)
  292.     /*@globals fileSystem @*/
  293.     /*@modifies ms, fileSystem @*/;
  294. protected void file_delmagic(struct magic *p, int type, size_t entries)
  295.     /*@globals fileSystem @*/
  296.     /*@modifies p, fileSystem @*/;
  297. protected void file_badread(struct magic_set *ms)
  298.     /*@modifies ms @*/;
  299. protected void file_badseek(struct magic_set *ms)
  300.     /*@modifies ms @*/;
  301. protected void file_oomem(struct magic_set *ms)
  302.     /*@modifies ms @*/;
  303. protected void file_error(struct magic_set *ms, int, /*@null@*/ const char *, ...)
  304.     /*@modifies ms @*/;
  305. protected void file_magwarn(struct magic_set *ms, /*@null@*/ const char *, ...)
  306.     /*@globals fileSystem @*/
  307.     /*@modifies fileSystem @*/;
  308. protected void file_mdump(struct magic *m)
  309.     /*@globals fileSystem @*/
  310.     /*@modifies m, fileSystem @*/;
  311. protected void file_showstr(FILE *fp, const char *, size_t)
  312.     /*@globals fileSystem @*/
  313.     /*@modifies fp, fileSystem @*/;
  314. protected size_t file_mbswidth(const char *)
  315.     /*@*/;
  316. /*@observer@*/
  317. protected const char *file_getbuffer(struct magic_set *ms)
  318.     /*@modifies ms @*/;
  319.  
  320. #ifndef HAVE_STRERROR
  321. extern int sys_nerr;
  322. extern char *sys_errlist[];
  323. #define strerror(e) \
  324.     (((e) >= 0 && (e) < sys_nerr) ? sys_errlist[(e)] : "Unknown error")
  325. #endif
  326.  
  327. #ifndef HAVE_STRTOUL
  328. #define strtoul(a, b, c)    strtol(a, b, c)
  329. #endif
  330.  
  331. #if 0 /* HACK -- we have snprintf and I don't want to plumb the autofoo */
  332. int snprintf(char *, size_t, const char *, ...);
  333. #endif
  334.  
  335. #if defined(HAVE_MMAP) && defined(HAVE_SYS_MMAN_H) && !defined(QUICK)
  336. #define QUICK
  337. #endif
  338.  
  339. #ifndef O_BINARY
  340. #define O_BINARY    0
  341. #endif
  342.  
  343. #define FILE_RCSID(id) \
  344. static const char *rcsid(const char *p) { \
  345.     return rcsid(p = id); \
  346. }
  347. #else
  348.  
  349. #endif /* __file_h__ */
  350.